package cn.icare.cms.controller;

import cn.icare.cms.feignclients.ADClient;
import cn.icare.cms.feignclients.AreaClient;
import cn.icare.cms.model.vo.rest.RespMsgVO;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@CrossOrigin
@RestController
@RequestMapping(value = "/rest/ad")
@Api(tags = "REST广告")
public class ADController extends BaseController {
    @Autowired
    private ADClient client;
    private final String FALL_STRING = "请求失败,服务繁忙!";


    /**
     * 查询所有轮播广告列表
     * @return
     */
    @GetMapping("/all")
    @HystrixCommand(fallbackMethod = "getAllFall") //增熔断器，以确保ADClient的服务挂了后，这个接口不会卡死
    @ApiOperation(value = "查询所有广告列表", notes = "查询所有广告列表")
    public ResponseEntity<RespMsgVO> getAll() {
        return this.client.getAllAD(0,1);
    }



    /**
     * 查询启用的弹窗广告
     * @return
     */
    @GetMapping("/popup")
    @HystrixCommand(fallbackMethod = "getPopupFall") //增熔断器，以确保ADClient的服务挂了后，这个接口不会卡死
    @ApiOperation(value = "查询所有广告列表", notes = "查询所有广告列表")
    public ResponseEntity<RespMsgVO> getPopupList() {
        return this.client.getAllAD(1,1);
    }

    //#region fallback methods
    private ResponseEntity<RespMsgVO> getAllFall() {
        return super.getRespFail("查询轮播广告失败，服务繁忙，请稍候再试");
    }


    private ResponseEntity<RespMsgVO> getPopupFall() {
        return super.getRespFail("查询弹窗广告失败，服务繁忙，请稍候再试");
    }

    //#endregion

}
